package com.audaque.springboot.foshanupload.web.raspcloud.controller;

import com.audaque.springboot.foshanupload.web.raspcloud.entity.User;
import com.audaque.springboot.foshanupload.web.raspcloud.req.ext.UserCreateReq;
import com.audaque.springboot.foshanupload.web.raspcloud.req.UserReq;
import com.audaque.springboot.foshanupload.web.raspcloud.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author chinaben
 * @since 2025-07-02
 */
@RestController

@Api(value = "用户 前端控制器")

@RequestMapping("/user")
public class UserController {


    @Autowired
    private IUserService iUserService;

    @PostMapping(value = "/page")
    @ApiOperation(value = "用户 分页查询")
    public Page<User> page(@RequestBody UserReq req) {
        QueryWrapper queryWrapper = new QueryWrapper(req);
        Page<User> page = iUserService.page(new Page<>(req.getPage(), req.getRows()), queryWrapper);
        return page;
    }


    @PostMapping(value = "/list")
    @ApiOperation(value = "用户 列表查询")
    public List<User> list(@RequestBody UserReq req) {
        QueryWrapper queryWrapper = new QueryWrapper(req);
        List<User> list = iUserService.list(queryWrapper);
        return list;
    }


    @GetMapping(value = "/{id}")
    @ApiOperation(value = "用户 单体查询")
    public User getById(@PathVariable("id") String id) {
        User one = iUserService.getById(id);
        return one;
    }

    @PostMapping(value = "/create")
    @ApiOperation(value = "用户 创建")
    public void create(@RequestBody @Valid UserCreateReq params) throws Exception {
        iUserService.save(params);
    }

    @PostMapping(value = "/delete/{id}")
    @ApiOperation(value = "用户 删除")
    public void delete(@PathVariable("id") Long id) {
        iUserService.removeById(id);
    }


    @PostMapping(value = "/enable/{id}/{enableYn}")
    @ApiOperation(value = "用户 是否启用（0禁用 1启用）")
    public void enable(@PathVariable("id") Long id, @PathVariable("enableYn") Boolean enableYn) {
        User entity = new User();
        entity.setId(id);
        entity.setEnableYn(enableYn);
        iUserService.updateById(entity);
    }


    @PostMapping(value = "/delFlag/{id}/{delFlag}")
    @ApiOperation(value = "用户 删除标志（0正常 1停用）")
    public void delFlag(@PathVariable("id") Long id, @PathVariable("delFlag") Boolean delFlag) {
        User entity = new User();
        entity.setId(id);
        entity.setDelFlag(delFlag);
        iUserService.updateById(entity);
    }


    @PostMapping(value = "/update")
    @ApiOperation(value = "用户 更新")
    public void update(@RequestBody @Valid User params) {
        iUserService.updateById(params);
    }


}
